/**
 * Copyright (c)  HeiFeiDeFengZheng.Co.Ltd. All rights reserved.
 **/
package com.dexing.hikaricp.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 功能描述：
 *
 * @author huifeidefengzheng
 * @since 2024-02-17
 *
 **/
public class JDBCDemo {
    public static void main(String[] args) {
        ResultSet resultSet = null;
        Statement statement = null;
        Connection connection = null;

        try {
            // 1. 注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2 获得连接
            String url = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&characterEncoding=utf-8";
            connection = DriverManager.getConnection(url, "root", "");

            // 3 获得执行sql语句的对象
            statement = connection.createStatement();

            // 4 执行sql语句，获取结果集
            resultSet = statement.executeQuery("select * from user");

            // 5 处理结果集
            while (resultSet.next()) {
                String cid = resultSet.getString("id");
                String cname = resultSet.getString("user_name");
                System.out.println(cid + "-------------" + cname);
            }


        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            try {
                if (statement != null) {
                    statement.close();
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            try {
                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
